package com.zty.user.controller;

import com.zty.redis.util.CacheUtil;
import com.zty.redis.util.RedisShareLockUtil;
import com.zty.redis.util.RedisUtil;
import com.zty.user.entity.po.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author ZTY
 * @Description 测试用的controller
 * @Date 11:52 2023-12-11
 **/

@RestController
@RequestMapping(value = "/api/user/")
public class TestController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RedisShareLockUtil redisShareLockUtil;

    @Resource
    private CacheUtil cacheUtil;

    @GetMapping("/test")
    public String test() {
        return "Hello World";
    }

    @GetMapping("/testRedis")
    public void testRedis() {
        redisUtil.set("name", "郑天宇");
    }

    @GetMapping("/testRedisLock")
    public void testRedisLock() {
        redisShareLockUtil.lock("zty", "123456", 30000L);
    }

    @GetMapping("/testLocalCache")
    public void testLocalCache() throws Exception {
        List<Long> skuIdList = new ArrayList<>();
        cacheUtil.getResult(skuIdList, "skuInfo.skuName", SkuInfo.class, (list) -> {
            Map<Long, SkuInfo> skuInfoMap = getSkuName(skuIdList);
            return skuInfoMap;
        });

        cacheUtil.getResult(skuIdList, "skuInfo.skuPrice", SkuPrice.class, (list) -> {
            Map<Long, SkuPrice> skuInfoMap = getSkuPrice(skuIdList);
            return skuInfoMap;
        });
    }

    //  获取商品名称
    public Map<Long, SkuInfo> getSkuName(List<Long> skuIds) {
        return Collections.emptyMap();
    }

    //  获取商品价格
    public Map<Long, SkuPrice> getSkuPrice(List<Long> skuIds) {
        return Collections.emptyMap();
    }

    //  创建内部类，编写二级缓存demo
    class SkuInfo {
        private Long id;
        private String name;
    }

    class SkuPrice {
        private Long id;
        private String price;
    }


    @PostMapping("/testQuery")
    public void testQuery(@RequestBody SysUser sysUser) throws Exception {

        //  如果将日期类型传给前端的话，前端会很难受。。。。应使用Json去传
        //  要采用标准的格式：2022-12-18 21:49:00
        //  前端传来的String类型时间格式我们也能自动将其标准化为我们的data为我们使用

        System.out.println(sysUser);

    }


}
